// pages/driver/meetingdetail/meetingdetail.js
var Tool = require("../../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDD: '',
    detail: '',
    sTime: '',
    eTime: '',
    winHeight: 0,
    iserror: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      IDD: options.mIdd,
    })
    this.getmeetingdetail()
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPullDownRefresh: function () {
    this.getmeetingdetail()
  },

  getmeetingdetail: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');
    wx.showToast({
      title: '正在加载...',
      icon: 'loading',
      duration: 10000
    });
    wx.request({
      url: 'https://www.lanshuicar.com/Server/Meeting/MtDetail.ashx',
      data: {
        type: 3,
        id: userInfo.Id,
        getType: 'getMeetDel',
        mIdd: that.data.IDD
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //隐藏Toast
          wx.hideToast();
          //显示错误信息
          that.showError('会议信息接口请求失败，请重试')
          that.setData({
            iserror: true
          });
          return
        }
        var data = tool.getRegularJson(res.data);

        if (data.data.length > 0) {
          that.setData({
            detail: data.data[0],
          });
          var sDate = that.data.detail.KSRQ.substring(0, 10)
          var sTime = that.data.detail.KSSJ.substring(11, 19)
          var eDate = that.data.detail.JSRQ.substring(0, 10)
          var eTime = that.data.detail.JSSJ.substring(11, 19)
          that.setData({
            sTime: sDate + ' ' + sTime,
            eTime: eDate + ' ' + eTime
          })
          // var logoURL = QRstring.substring(0, 21)

          console.log(that.data.detail)
        }

        console.log(data)
      }, fail: function (res) {
        console.log(res)
      }, complete: function (res) {
        wx.hideToast()
        wx.stopPullDownRefresh();
      }
    })
  },
  //定位签到跳转
  gosign: function () {

    if (this.data.detail.ZBJD == '' || this.data.detail.ZBWD == '') {
      wx.showToast({
        title: '会议地点尚未确定',
        icon: 'none'
      })
      return
    }

    wx.navigateTo({
      url: '../signingMap/signingMap?longitude=' + this.data.detail.ZBJD + '&latitude=' + this.data.detail.ZBWD + '&radius=' + this.data.detail.YQFW + '&QDZT=' + this.data.detail.QDZT + '&QLZT=' + this.data.detail.QLZT + '&mIDD=' + this.data.detail.IDD,
    })
  },

  //扫码签到跳转
  gosignQR: function (e) {
    var that = this
    var tag = e.currentTarget.id//tag是1签到，tag是2签离
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res)
        var QRstring = res.result
        //处理二维码方法
        that.dealQRcode(QRstring, tag)
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },

  dealQRcode: function (QRstring, tag) {
    //域名标记string
    var logoURL = QRstring.substring(0, 21)
    console.log(logoURL)
    //识别域名标记
    if (logoURL == 'http://lanshuicar.com') {
      console.log('标记识别正确')
      var onlynum = QRstring.substring(52, 88)
      console.log(onlynum)

      //调用接口操作
      this.doQRsign(onlynum, tag)

    } else {
      wx.showToast({
        title: '您扫描的二维码似乎是错误的,请重新扫描',
        icon: 'none'
      })

    }

  },

  doQRsign: function (onlynum, tag) {
    var that = this
    var userInfo = wx.getStorageSync('userInfo')
    var userType = wx.getStorageSync('userType')
    var tel = userInfo.sTel
    var ashx = ''
    var msg = ''
    //tag为1 签到接口url
    if (tag == 1) {
      ashx = 'Meeting/DriverSignNew.ashx'
      msg = '签到成功'
    }
    //tag为2 签离接口url
    else {
      console.log('2')
      ashx = 'Meeting/LeaveSignNew.ashx'
      msg = '签离成功'
    }
    console.log(ashx)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/' + ashx,
      data: {
        type: '3',
        id: userInfo.Id,
        address: that.data.detail.RWDD,
        long: that.data.detail.ZBJD,
        lat: that.data.detail.ZBWD,
        mIDD: that.data.detail.IDD,
        deviceNum: tel,
        onlynum: onlynum,
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          setTimeout(function () {
            wx.showToast({
              title: msg,
            })
          }, 1500)
          that.getmeetingdetail()
          var pages = getCurrentPages()
          var backpage = pages[pages.length - 2]
          backpage.setData({
            backtag: 1,
          });


        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }

      },
      fail: function (res) {
        console.log(res)
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })

      }
    })
  },


  confirmReceive: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo')
    wx.request({
      url: 'https://www.lanshuicar.com/Server/Meeting/QueryOptHdl.ashx',
      data: {
        mIDD: that.data.detail.IDD,
        type: '3',
        id: userInfo.Id,
      },
      method: 'GET',
      success: function (res) {
        if (res.data.code == 0) {
          console.log(res)
          wx.showToast({
            title: '确认成功',
          })
          that.getmeetingdetail()
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      },

    })
  },

  hasConfirm: function () {
    wx.showToast({
      title: '已确认接收，无需重复确认',
      icon: 'none',
    })
  },

})